﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class CTHANHDAODAO:IData<CTHANHDAO> 
 	{
		public CTHANHDAODAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref CTHANHDAO item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + CTHANHDAO.c_ID;
			 p.Value = item.ID;
			 Object obj = item.ID; 
			int i = connec.ExecuteNonQuery("sp_CTHANHDAO", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + CTHANHDAO.c_CVID, item.CVID), new SqlParameter("@" + CTHANHDAO.c_NVID, item.NVID),
				 new SqlParameter("@" + CTHANHDAO.c_NAM, item.NAM), new SqlParameter("@" + CTHANHDAO.c_CV_CHINH, item.CV_CHINH),
				 new SqlParameter("@" + CTHANHDAO.c_GHICHU, item.GHICHU));
			if(t == 0)
				item.ID = Convert.ToInt64(obj);
			return i;
		}

		public override CTHANHDAO GetItem(IDataReader dr)
		{
			CTHANHDAO obj = new CTHANHDAO();
			obj.ID = Convert.ToInt64(dr[CTHANHDAO.c_ID]);
			obj.CVID = Convert.ToInt64(dr[CTHANHDAO.c_CVID]);
			obj.NVID = Convert.ToInt64(dr[CTHANHDAO.c_NVID]);
			obj.NAM = Convert.ToInt32(dr[CTHANHDAO.c_NAM]);
			obj.CV_CHINH = Convert.ToBoolean(dr[CTHANHDAO.c_CV_CHINH]);
			obj.GHICHU = Convert.ToString(dr[CTHANHDAO.c_GHICHU]);
			return obj;
		}

		public override List<CTHANHDAO> GetAll()
		{
			return GetAll("sp_GetAllCTHANHDAO", CommandType.StoredProcedure);
		}

		public override CTHANHDAO GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.ID == Convert.ToInt64(id));
		}
		public override CTHANHDAO GetItemById(CTHANHDAO item){
			throw new NotImplementedException();
		}

	}

 }
